# -*- coding: utf-8 -*-
from scrapy import Spider, Request
from zhaopin.items import ZhaopinItem


class ZhilianSpider(Spider):
    name = 'zhilian'
    allowed_domains = ['sou.zhaopin.com']
    start_urls = ['http://sou.zhaopin.com/']

    start_url = 'https://sou.zhaopin.com/jobs/searchresult.ashx?bj={bj}&jl={jl}&sm={sm}&p={p}&isfilter={isfilter}&fl={fl}&isadv={isadv}'

    bj = '2080000'
    jl = '西安'
    sm = '1'
    p = '1'
    isfilter = '1'
    fl = '854'
    isadv = '0'

    def start_requests(self):
        yield Request(url=self.start_url.format(bj=self.bj, jl=self.jl, sm=self.sm, p=self.p, isfilter=self.isfilter, fl=self.fl, isadv=self.isadv), callback=self.parse)

    def parse(self, response):
        list = response.css('#newlist_list_content_table > .newlist_detail')
        page = response.css('body > div.main > div.search_newlist_main > div.newlist_main > form > div.clearfix > div.newlist_wrap.fl > div.pagesDown > ul')

        for cell in list:
            item = ZhaopinItem()
            item['title'] = cell.css('ul > .newlist_deatil_first > div > a::text').extract_first()
            item['url'] = cell.css('ul > .newlist_deatil_first > div > a::attr(href)').extract_first()
            item['company'] = cell.css('ul > .newlist_deatil_three > a:first-child::text').extract_first()
            item['site'] = cell.css('ul > .newlist_deatil_two > span:first-child::text').extract_first()
            item['sarlary'] = cell.css('ul > .newlist_deatil_two > span:last-child::text').extract_first()

            yield item

        nextPage = page.css('a.next-page::attr(href)').extract_first()
        if nextPage:
            yield Request(nextPage, self.parse)
